package com.example.administrator.startproject_xmr112;

import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.startproject_xmr112.bean.WeatherInfo;
import com.example.administrator.startproject_xmr112.iface.WeatherListener;
import com.example.administrator.startproject_xmr112.model.WeatherModel;
import com.example.administrator.startproject_xmr112.model.Weather51Model;


public class MainActivity extends AppCompatActivity implements WeatherListener,View.OnClickListener{
    private EditText etcity;
    private Button btnfind1,btnfind2;
    private TextView tvcityname;
    private TextView tvcitytemp;
    private TextView tvcityWD;
    private TextView tvcityWS;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private void init()
    {
        etcity=(EditText)findViewById(R.id.editText);
        btnfind1=(Button)findViewById(R.id.button1);
        btnfind2=(Button)findViewById(R.id.button2);
        tvcityname=(TextView)findViewById(R.id.textView2);
        tvcitytemp=(TextView)findViewById(R.id.textView);
        tvcityWD=(TextView)findViewById(R.id.textView3);
        tvcityWS=(TextView)findViewById(R.id.textView4);
        btnfind1.setOnClickListener(this);
        btnfind2.setOnClickListener(this);
    }


    @Override
    public void onResponse(WeatherInfo weatherinfo) {
        if (weatherinfo!=null)
        {
            tvcityname.setText("城市："+weatherinfo.getCity());
            tvcitytemp.setText("温度："+weatherinfo.getTemp());
            tvcityWD.setText("风向："+weatherinfo.getWD());
            tvcityWS.setText("风力："+weatherinfo.getWS());
        }
        else

            tvcityname.setText("未知");
    }

    @Override
    public void onFail(String msg) {
        tvcityname.setText(msg);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
        {
            case R.id.button1:
                System.out.println("----- 按钮1被点击");
                WeatherModel weatherModel=new WeatherModel();
                weatherModel.getWeather(etcity.getText().toString(),this);
                break;
            case R.id.button2:
                System.out.println("-----按钮2被点击");
                Weather51Model weather51Model=new Weather51Model();
                weather51Model.getWeather(etcity.getText().toString(),this);
                break;
        }
    }
}
